package com.zx.deploy.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zx.deploy.common.exception.BusinessException;
import com.zx.deploy.common.utils.CommonUtil;
import com.zx.deploy.common.utils.SpringContextUtil;
import com.zx.deploy.config.DeployConfig;
import com.zx.deploy.model.SpeedProgress;
import com.zx.deploy.service.DataBaseService;
import com.zx.deploy.service.SoftService;
import com.zx.deploy.service.impl.SoftServiceImpl;

import io.swagger.annotations.Api;


@Api(value="部署服务")
@Controller
public class SoftController {
	
	@Autowired
	protected SoftService softService;
	/**
	 * 环境变量
	 */
	@Autowired
	protected DeployConfig config;
	/**
	 * 数据库操作impl EnvDataBaseService JdbcDataBaseService
	 */
	@Resource(name="${mysql.impl.className}")
	protected DataBaseService  dataBaseService;
	
	/**
	 * 升级服务
	 * @return
	 */
	@GetMapping("/install")
	@ResponseBody
	public Object installServer() {
		SpeedProgress speed = new SpeedProgress();
		softService.install(config, speed);
		return speed;
	}
	
	
	
	/**
	 * 从备份中恢复
	 * @return
	 */
	@GetMapping("/recoveryPackage")
	@ResponseBody
	public Object recoveryPackage(@RequestParam(required = true)String fileName) {
		SpeedProgress speed = new SpeedProgress();
		try {
			softService.recoveryPackage(config, speed,fileName);
		} catch (BusinessException e) {
			speed.setInfo("错误终止", e.getMessage());
			speed.setStatus(0);
		} catch (Exception e) {
			speed.setInfo("错误终止", "发生错误终止");
			speed.setStatus(0);
		}
		return speed;
	}
	
	

	
	/**
	 * 解压服务
	 * @return
	 */
	@GetMapping("/unPackage")
	@ResponseBody
	public Object unPackage() {
		SpeedProgress speed = new SpeedProgress();
		softService.unPackage(config, speed);
		return speed;
	}
	
	
	/**
	 * 停止服务
	 * @return
	 */
	@GetMapping("/stopServer")
	@ResponseBody
	public Object stopServer() {
		SpeedProgress speed = new SpeedProgress();
		softService.stopServer(config, speed);
		speed.setInfo("服务已停止");
		return speed;
	}
	
	
	/**
	 *启动服务
	 * @return
	 */
	@GetMapping("/startServer")
	@ResponseBody
	public Object startServer() {
		SpeedProgress speed = new SpeedProgress();
		softService.startServer(config, speed);
		speed.setInfo("服务启动完成");
		return speed;
	}
	
	
	/**
	 *重启服务
	 * @return
	 */
	@GetMapping("/restartServer")
	@ResponseBody
	public Object restartServer() {
		SpeedProgress speed = new SpeedProgress();
		try {
			softService.stopServer(config, speed);
			CommonUtil.sleep(5000);
			softService.startServer(config, speed);
			CommonUtil.sleep(10000);
			speed.setInfo("服务启动完成");
		} catch (BusinessException e) {
			speed.setInfo("错误终止", e.getMessage());
			speed.setStatus(0);
		} catch (Exception e) {
			speed.setInfo("错误终止", "发生错误终止");
			speed.setStatus(0);
		}
		return speed;
	}
	
	
	/**
	 *清除服务(去掉全部或者某一项服务)
	 * @return
	 */
	@GetMapping("/cleanServer")
	@ResponseBody
	public Object cleanServer() {
		SpeedProgress speed = new SpeedProgress();
		try {
			softService.stopServer(config, speed);
			CommonUtil.sleep(5000);
			  //TODO 未完成
		} catch (BusinessException e) {
			speed.setInfo("错误终止", e.getMessage());
			speed.setStatus(0);
		} catch (Exception e) {
			speed.setInfo("错误终止", "发生错误终止");
			speed.setStatus(0);
		}
		return speed;
	}
	
	/**
	 *备份包
	 *@param war 如果多个包 逗号分隔
	 * @return
	 */
	@GetMapping("/backupWar")
	@ResponseBody
	public Object backupWar(@RequestParam(required = true) String war) {
		SpeedProgress speed = new SpeedProgress();
		List<String> warsName = Arrays.asList(war.split(","));
		softService.backupWar(config, warsName, speed);
		return speed;
	}
	
	
	/**
	 *清理 备份文件
	 * @return
	 */
	@GetMapping("/clearBackFile")
	@ResponseBody
	public Object clearBackFile(@RequestParam(defaultValue = "10", required = false) Integer num) {
		return softService.clearBackFile(config, num);
	}
	
	
	/**
	 *执行脚本
	 * @return
	 */
	@GetMapping("/execSql")
	@ResponseBody
	public Object execSql() {
		SpeedProgress speed = new SpeedProgress();
		try {
			softService.executeSql(config, speed);
		} catch (BusinessException e) {
			speed.setInfo("错误终止", e.getMessage());
			speed.setStatus(0);
		} catch (Exception e) {
			speed.setInfo("错误终止", "发生错误终止");
			speed.setStatus(0);
		}
		return speed;
	}
	
	
	/**
	 *备份数据库
	 * @return
	 */
	@GetMapping("/backupDb")
	@ResponseBody
	public Object backupDb() {
		SpeedProgress speed = new SpeedProgress();
		try {
			speed = dataBaseService.backupDatabase(config);
		} catch (BusinessException e) {
			speed.setInfo("错误终止", e.getMessage());
			speed.setStatus(0);
		} catch (Exception e) {
			speed.setInfo("错误终止", "发生错误终止");
			speed.setStatus(0);
		}
		return speed;
	}
	
	
	/**
	 *恢复数据库
	 * @return
	 */
	@GetMapping("/rollbackDb")
	@ResponseBody
	public Object rollbackDb(@RequestParam(required = true) String callBackSql ) {
		SpeedProgress speed = new SpeedProgress();
		try {
			speed = dataBaseService.rollbackDatabase(callBackSql, config);
		} catch (BusinessException e) {
			speed.setInfo("错误终止", e.getMessage());
			speed.setStatus(0);
		} catch (Exception e) {
			speed.setInfo("错误终止", "发生错误终止");
			speed.setStatus(0);
		}
		return speed;
	}
	
	
	@GetMapping("/sql")
	@ResponseBody
	public Object evn () {
		SpeedProgress speed = new SpeedProgress();
		List<String> list =new SoftServiceImpl().updateSqls(config, speed);
		 return config;
	}
	
}
